package xyz.robinbingo.effectivejava.chapter02.item06;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.lang.Console;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.stream.LongStream;

/**
 * @author Robin
 * @date 2019/03/13 17:59
 **/
@RunWith(SpringRunner.class)
@SpringBootTest
public class Item06 {

    @Test
    public void test00() {
        String s = new String("bikini"); // DON'T DO THIS!
        String s1 = "bikini";
        String s2 = "bikini";
        System.out.println(s.equals(s1));
        System.out.println(s == s1);
        System.out.println(s1 == s2);
    }

    @Test
    public void test01() {
        Boolean aTrue = new Boolean("true");
        Boolean aTrue1 = Boolean.valueOf("true");
    }

    @Test
    public void test02() {
        TimeInterval timer = DateUtil.timer();
        Console.log(sum());
        Console.log("1:{}", timer.interval());

        timer.restart();
        Console.log(sum1());
        Console.log("2:{}", timer.interval());
    }

    // Hideously slow! Can you spot the object creation?
    private static long sum() {
        Long sum = 0L;
        for (long i = 0; i <= Integer.MAX_VALUE; i++) {
            sum += i;
        }
        return sum;
    }

    private static long sum1() {
        return LongStream.rangeClosed(0, Integer.MAX_VALUE).sum();
    }

}
